package 课上练习0419_scala_collection

object Scala_05Set {
  def main(args: Array[String]): Unit = {
    // 无序，不重复
    println("===============创建Set====================")
    val s1 = Set(1, 2, 3, 4, 46, 4, 4, 4)
    val s2 = Set(2, 9, 13, 4, 47)
    println("s1: " + s1)
    println("===============操作Set====================")
    println("交集：") //两个都有
    println(s1.&(s2))
    println(s1.intersect(s2))

    println("差集：") //s1有，s2没有
    println(s1.&~(s2))
    println(s1.diff(s2))

    println("并集：") //s1和s2所有的元素合并，去重
    println(s1.union(s2))
    println("===============函数Set====================")
    println(s"max: ${s1.max}")
    println(s"min: ${s1.min}")
    println(s"head: ${s1.head}") // 46
    println(s"tail: ${s1.tail}") // 除了head之外剩下的元素，(1, 2, 3, 4)
    println(s"isEmpty: ${s1.isEmpty}")
    println(s"count: ${s1.count(_ > 0)}")
    println("===============扩展Set====================")
    // 并行集合
    val result1 = (1 to 100000).map(_ => Thread.currentThread().getName).distinct
    import scala.collection.parallel.CollectionConverters._
    val result2 = (1 to 100000).par.map(_ => Thread.currentThread().getName).distinct
    println("线程数: " + result1.length) // 1, main
    println("线程数: " + result2.length) // 16, scala-execution-context-global-xx
  }
}
